package service

import (
	"zsc-boot/app/customer/model"
	"zsc-boot/app/customer/model/request"
	"zsc-boot/app/customer/model/response"
	"zsc-boot/core/orm"
)

type ReportSvc struct {
}

func (r *ReportSvc) ChannelLevelReport(req request.ChannelReportReq) (resList []response.ChannelLevelReportRes, err error) {
	db := orm.GetDb().Model(&model.CustomerImportLog{})
	db = req.BuildWhere(db)
	if err = db.Group("channel_id").Select("channel_id,count(*) as customer_count").
		Preload("ChannelInfo").Find(&resList).Error; err != nil {
		return
	}

	var customer []response.CustomerChannelLevel
	if err = orm.GetDb().Model(&model.Customer{}).Where("id in (?)", req.BuildWhere(orm.GetDb().Model(&model.CustomerImportLog{}).Select("customer_id")).Select("customer_id")).Find(&customer).Error; err != nil {
		return
	}
	for _, v := range customer {
		for k, _ := range resList {
			if resList[k].ChannelId == v.ChannelId {
				switch v.Level {
				case 0:
					resList[k].ZeroLevel++
				case 1:
					resList[k].OneLevel++
				case 2:
					resList[k].TwoLevel++
				}
			}
		}
	}
	return
}

func (r *ReportSvc) ChannelReport(req request.ChannelReportReq) (resList []response.ChannelReportRes, err error) {
	db := orm.GetDb().Model(&model.CustomerImportLog{})
	db = req.BuildWhere(db)
	if err = db.Group("channel_id").Select("channel_id,count(*) as customer_count").Preload("ChannelInfo").Find(&resList).Error; err != nil {
		return
	}

	var customerTwoLevel []response.ChannelReportRes
	if err = orm.GetDb().Model(&model.Customer{}).
		Where("id in (?)", req.BuildWhere(orm.GetDb().Model(&model.CustomerImportLog{})).Select("customer_id")).
		Where("level = ?", 2).
		Group("channel_id").Select("channel_id,count(*) as customer_count").Find(&customerTwoLevel).Error; err != nil {
		return
	}
	//填充
	for k, _ := range resList {
		for _, v := range customerTwoLevel {
			if v.ChannelId == resList[k].ChannelId {
				resList[k].TwoLevel = v.TwoLevel
			}
		}
	}

	var orderCount []model.Order

	if err = orm.GetDb().Model(&model.Order{}).
		Where("customer_id in (?)", req.BuildWhere(orm.GetDb().Model(&model.CustomerImportLog{})).Select("customer_id")).
		Preload("OrderAdds").Find(&orderCount).Error; err != nil {
		return
	}
	//填充
	for _, v := range orderCount {
		for k, _ := range resList {
			if resList[k].ChannelId == v.ChannelId {
				var money int64
				for _, m := range v.OrderAdds {
					money += int64(m.Money)
				}
				resList[k].OrderCount++
				resList[k].OrderMoneyCount += int64(v.Money)
				resList[k].OrderAddMoneyCount += money
			}
		}
	}
	return
}

func (r *ReportSvc) UserReport(req request.ChannelReportReq) (resList []response.UserReportRes, err error) {
	db := orm.GetDb().Model(&model.CustomerImportLog{})
	db = req.BuildWhere(db)
	if err = db.Group("user_id").Select("user_id,count(*) as customer_count").Preload("UserInfo").
		Find(&resList).Error; err != nil {
		return
	}

	var customerTwoLevel []response.UserReportRes
	if err = orm.GetDb().Model(&model.Customer{}).
		Where("id in (?)", req.BuildWhere(orm.GetDb().Model(&model.CustomerImportLog{})).Select("customer_id")).
		Where("level = ?", 2).Group("user_id").
		Select("user_id,count(*) as customer_count").Preload("UserInfo").Find(&customerTwoLevel).Error; err != nil {
		return
	}
	//填充
	for k, _ := range resList {
		for _, v := range customerTwoLevel {
			if v.UserId == resList[k].UserId {
				resList[k].TwoLevel = v.TwoLevel
			}
		}
	}

	var orderCount []model.Order

	if err = orm.GetDb().Model(&model.Order{}).
		Where("customer_id in (?)", req.BuildWhere(orm.GetDb().Model(&model.CustomerImportLog{})).Select("customer_id")).
		Preload("OrderAdds").Find(&orderCount).Error; err != nil {
		return
	}
	//填充
	for _, v := range orderCount {
		for k, _ := range resList {
			if resList[k].UserId == v.UserId {
				var money int64
				for _, m := range v.OrderAdds {
					money += int64(m.Money)
				}
				resList[k].OrderCount++
				resList[k].OrderMoneyCount += int64(v.Money)
				resList[k].OrderAddMoneyCount += money
			}
		}
	}
	return
}
